package com.nova.scenic.projectlibs.util.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.nova.scenic.projectlibs.util.Constant;
import com.nova.scenic.projectlibs.util.debug.MyLog;
import com.nova.scenic.projectlibs.util.lifecyclehelper.LifeCycleHelper;
import com.nova.scenic.projectlibs.util.lifecyclehelper.LifeCycleLogHelper;

import java.io.FileDescriptor;
import java.io.PrintWriter;

public class BaseFragment extends android.support.v4.app.Fragment implements Constant {

    LifeCycleHelper lifeCycleHelper = new LifeCycleLogHelper(null){
        @Override
        public String getObjectInstance() {
            return "@"+ MyLog.getInstanceName(this);
        }
    };


    @Override
    public void onAttach(Context activity) {
        super.onAttach(activity);
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onAttach(activity);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onAttach(activity);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (lifeCycleHelper != null) {
            lifeCycleHelper.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onDetach();
        }
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onCreate(savedInstanceState);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onDestroy();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onResume();
        }

    }

    @Override
    public void onPause() {
        super.onPause();
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onPause();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onStart();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onStop();
        }
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onViewStateRestored(savedInstanceState);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onSaveInstanceState(outState);
        }
    }

    @Override
    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
        if (lifeCycleHelper != null) {
            lifeCycleHelper.dump(prefix, fd, writer, args);
        }
    }


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onCreateContextMenu(menu, v, menuInfo);
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onCreateOptionsMenu(menu, inflater);
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onViewCreated(view, savedInstanceState);
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onLowMemory();
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (lifeCycleHelper != null) {
            if (lifeCycleHelper.onOptionsItemSelected(item)) {
                return true;
            }
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onDestroyView();
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onDestroyView();
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onCreateView(inflater, container, savedInstanceState);
        }
        return super.onCreateView(inflater, container, savedInstanceState);

    }

    @Override
    public void onDestroyOptionsMenu() {
        super.onDestroyOptionsMenu();
    }

    public String getName() {
        return this.getClass().getSimpleName() + "(" + hashCode() + ")" + "\t";
    }


}